/**
 * @file protobuf_serializer_test.cpp
 * @brief Protobuf 序列化器测试用例
 * 
 * 该文件演示如何使用 ProtobufSerializer 进行消息的序列化和反序列化
 */

#include <iostream>
#include <vector>
#include <memory>
#include <cassert>

#include "serializer/protobuf_serializer.h"
#include "user_data.pb.h"  // 生成的protobuf头文件

/**
 * @brief 创建测试用户数据
 */
comm::UserData createTestUserData() {
    comm::UserData user;
    
    // 设置基本字段
    user.set_id(12345);
    user.set_name("张三");
    user.set_score(95.5f);
    user.set_timestamp(1703808000);  // 2023-12-28 20:00:00
    user.set_email("zhangsan@example.com");
    
    // 添加标签
    user.add_tags("开发者");
    user.add_tags("C++");
    user.add_tags("后端");
    
    // 设置地址信息
    auto* address = user.mutable_address();
    address->set_street("中关村大街1号");
    address->set_city("北京");
    address->set_country("中国");
    address->set_postal_code(100080);
    
    return user;
}

/**
 * @brief 验证用户数据是否正确
 */
bool verifyUserData(const comm::UserData& original, const comm::UserData& deserialized) {
    if (original.id() != deserialized.id()) {
        std::cout << "ID 不匹配: " << original.id() << " vs " << deserialized.id() << std::endl;
        return false;
    }
    
    if (original.name() != deserialized.name()) {
        std::cout << "姓名不匹配: " << original.name() << " vs " << deserialized.name() << std::endl;
        return false;
    }
    
    if (std::abs(original.score() - deserialized.score()) > 0.001f) {
        std::cout << "分数不匹配: " << original.score() << " vs " << deserialized.score() << std::endl;
        return false;
    }
    
    if (original.timestamp() != deserialized.timestamp()) {
        std::cout << "时间戳不匹配: " << original.timestamp() << " vs " << deserialized.timestamp() << std::endl;
        return false;
    }
    
    if (original.email() != deserialized.email()) {
        std::cout << "邮箱不匹配: " << original.email() << " vs " << deserialized.email() << std::endl;
        return false;
    }
    
    // 检查标签
    if (original.tags_size() != deserialized.tags_size()) {
        std::cout << "标签数量不匹配: " << original.tags_size() << " vs " << deserialized.tags_size() << std::endl;
        return false;
    }
    
    for (int i = 0; i < original.tags_size(); i++) {
        if (original.tags(i) != deserialized.tags(i)) {
            std::cout << "标签不匹配 [" << i << "]: " << original.tags(i) << " vs " << deserialized.tags(i) << std::endl;
            return false;
        }
    }
    
    // 检查地址信息
    const auto& orig_addr = original.address();
    const auto& deser_addr = deserialized.address();
    
    if (orig_addr.street() != deser_addr.street() ||
        orig_addr.city() != deser_addr.city() ||
        orig_addr.country() != deser_addr.country() ||
        orig_addr.postal_code() != deser_addr.postal_code()) {
        std::cout << "地址信息不匹配" << std::endl;
        return false;
    }
    
    return true;
}

/**
 * @brief 测试基本序列化和反序列化功能
 */
bool testBasicSerialization() {
    std::cout << "\n=== 测试基本序列化和反序列化功能 ===" << std::endl;
    
    try {
        // 创建序列化器
        ProtobufSerializer serializer;
        
        // 创建测试数据
        comm::UserData original_user = createTestUserData();
        
        std::cout << "原始用户数据:" << std::endl;
        std::cout << "  ID: " << original_user.id() << std::endl;
        std::cout << "  姓名: " << original_user.name() << std::endl;
        std::cout << "  分数: " << original_user.score() << std::endl;
        std::cout << "  邮箱: " << original_user.email() << std::endl;
        std::cout << "  标签数量: " << original_user.tags_size() << std::endl;
        
        // 序列化
        auto serialized_data = serializer.serialize(original_user);
        if (serialized_data.empty()) {
            std::cout << "序列化失败!" << std::endl;
            return false;
        }
        
        std::cout << "序列化成功，数据大小: " << serialized_data.size() << " bytes" << std::endl;
        
        // 反序列化
        comm::UserData deserialized_user;
        bool deserialize_success = serializer.deserialize(serialized_data, deserialized_user);
        
        if (!deserialize_success) {
            std::cout << "反序列化失败!" << std::endl;
            return false;
        }
        
        std::cout << "反序列化成功" << std::endl;
        
        // 验证数据
        if (!verifyUserData(original_user, deserialized_user)) {
            std::cout << "数据验证失败!" << std::endl;
            return false;
        }
        
        std::cout << "数据验证成功!" << std::endl;
        return true;
        
    } catch (const std::exception& e) {
        std::cout << "异常: " << e.what() << std::endl;
        return false;
    }
}

/**
 * @brief 测试 JSON 转换功能
 */
// bool testJsonConversion() {
//     std::cout << "\n=== 测试 JSON 转换功能 ===" << std::endl;
    
//     try {
//         // 创建序列化器
//         ProtobufSerializer serializer;
        
//         // 创建测试数据
//         UserData original_user = createTestUserData();
        
//         // JSON功能已删除 - 跳过此测试
//         std::cout << "JSON 转换功能已删除 - 跳过测试" << std::endl;
//         return true;
        
//     } catch (const std::exception& e) {
//         std::cout << "异常: " << e.what() << std::endl;
//         return false;
//     }
// }

/**
 * @brief 测试序列化器信息
 */
bool testSerializerInfo() {
    std::cout << "\n=== 测试序列化器信息 ===" << std::endl;
    
    try {
        ProtobufSerializer serializer;
        
        std::cout << "序列化器名称: " << serializer.name() << std::endl;
        std::cout << "序列化器版本: " << serializer.version() << std::endl;
        
        return true;
        
    } catch (const std::exception& e) {
        std::cout << "异常: " << e.what() << std::endl;
        return false;
    }
}

/**
 * @brief 测试错误处理
 */
bool testErrorHandling() {
    std::cout << "\n=== 测试错误处理 ===" << std::endl;
    
    try {
        ProtobufSerializer serializer;
        
        // 测试空数据反序列化
        std::vector<uint8_t> empty_data;
        comm::UserData user;
        bool result = serializer.deserialize(empty_data, user);
        
        if (result) {
            std::cout << "空数据反序列化应该失败，但返回了成功!" << std::endl;
            return false;
        }
        
        std::cout << "空数据反序列化正确返回失败" << std::endl;
        
        // 测试无效数据反序列化
        std::vector<uint8_t> invalid_data = {0x00, 0x01, 0x02, 0x03};
        result = serializer.deserialize(invalid_data, user);
        
        if (result) {
            std::cout << "无效数据反序列化应该失败，但返回了成功!" << std::endl;
            return false;
        }
        
        std::cout << "无效数据反序列化正确返回失败" << std::endl;
        
        return true;
        
    } catch (const std::exception& e) {
        std::cout << "异常: " << e.what() << std::endl;
        return false;
    }
}

int main() {
    std::cout << "Protobuf 序列化器测试程序" << std::endl;
    std::cout << "=================================" << std::endl;
    
    bool all_tests_passed = true;
    
    // 运行所有测试
    all_tests_passed &= testSerializerInfo();
    all_tests_passed &= testBasicSerialization();
    // all_tests_passed &= testJsonConversion();
    all_tests_passed &= testErrorHandling();
    
    std::cout << "\n=================================" << std::endl;
    if (all_tests_passed) {
        std::cout << "✓ 所有测试通过!" << std::endl;
        return 0;
    } else {
        std::cout << "✗ 部分测试失败!" << std::endl;
        return 1;
    }
}
